PIZZA Current compiler version: 0.39d
A substantial companion to Java
Introduction to Java and Pizza

Contents
Home
Mirrors
FAQ

Distribution
Support
Documents
-Tutorial index
-Examples
-API
-Definition

Applications
Users

People
Links

Week 2: Java Specifics

  • jhead
  • throw, try, exception handling
  • name spaces, packages, imports
  • access specifiers
  • inheritance
  • overriding and overloading
  • interfaces

  • Scope rules

    all variables need to be declared

    Local variables visible from their point of declaration until end of block

    Class fields and methods visible everywhere in class


    Exceptions

  • Exceptions signal uncommon conditions
  • Exceptions are objects, need to be created explicitly.
  • Exception types inherit from java.lang.Exception
  • Throwing an exception aborts execution. Example
  • Often, creating and throwing an exception are combined:

  • Try-Catch

    Exceptions can be "caught" by a try statement. General form:

            try {
                ...
            } catch (Exception1 ex) {
                ...
            } catch (Exception2 ex) {
                ...
            }

    Catch clauses are tried in sequence. First clause that matches the thrown exception is executed.


    Try-Finally

    An optional finally section in the try statement contains code that will be executed whether an exception was raised or not.

    Example

    This is equivalent to:


    Throws Clauses

    Uncaught exceptions have to be declared in a method signature:

    Only exceptions: Exceptions inheriting from RuntimeException or Error


    Packages

            pizza.compiler.Main
          java.lang.System


    Name Spaces


    Package <=> Directory

    The package of a class influences where the class is searched.

    Interpreter and compiler will look for a class such as pizza.compiler.Main in the directory pizza/compiler/Main.

    The root of the search is a directory on the CLASSPATH.

    Setting the compiler's -d option will alsways ensure that classes end up in the right directory.


    Imports

    import java.io.File lets you use File instead of java.io.File
    import java.io.* makes available all files in java.io in unqualified form.


    The Standard Library

    Organized as packages:

    java.lang Basis classes: Object, String, System, Math
    java.io Input/Output
    java.util Utility classes: hash tables, vectors, enumerations
    java.applet Helper classes for applet execution
    java.awt Platform-independent graphical user interface
    java.net Network programming


    Class Extension


    Subtyping



    Typecasts and typetests


    Subtyping and Arrays

  • Subtyping translates to arrays:
  • if X extends Y then also X[] extends Y[]
  • This is actually unsound!
  • Example:
  • Morale: Subtyping should not extend to types with mutable fields.

  • Inheritance

  • An extending class inherits all fields and methods of the class it extends.
  • It may re-define some inherited methods; this is called overriding.
  • Example:
  • Overrding is dynamic - the new definition will always be used, no matter what the static type.

  • The Object Class

  • Class java.lang.Object forms the root of the inhertiance hierarchy.
  • Every class is directly or indirectly a subclass of Object
  • Object defines several useful methods, which are often overridden in subclasses. These include:

  • Overloading

  • It is possible to use the same method name with different argument lists
  • In this case, the argument types determine which method will be chosen.
  • If there is a method whoch most closely matches each argument, that method will be chosen.
  • Unlike overriding, overloading uses the static types of the argument.
  • Overriding applies only if the argument types for methods in sub- and superclass are exactly the same.

  • Access Modifiers

  • Field and method declarations may be prefixed with an access modifier
  • [public | private | protected] type name ...
  • public

      visible everywhere

    protected

      visible in all subclasses and in same package

    (none)

      visible in same package

    private

      visible only in same class


    Other Field- and Method-Declaration Modifiers


    Class Modifiers

  • [abstract | public | final] class ...
  • abstract
    • can contain abstract or unimplemented methods
    • Instances of abstract classes cannot be created
    public
    • class is visible outside package
    • javac: At most one public class per compilation unit; classname and filename must be the same.
    final
    • no subclasses allowed.


    Interfaces

    
    
    


    Interfaces

  • All methods in an interface are implicitly public and abstract
  • All constants are implicitly public, static, final
  • Interfaces can inherit from several other interfaces.
  • "Implementing" an interface with nothing but constants is a convenient hack for accesing constants without the class qualifier.

  • Example: Sets


    An Implementation of Sets:


    Empty Lists

        public class Empty extends List {
            NonEmpty(Object head; List rest) {
                this.head = head;
                this.rest = rest;
            }
        }


    Assignment

    An interface for Tables is given as follows:



    Java is a trademark of Sun Microsystems.
    Comments and bug reports to the Pizza Group, pizza@cis.unisa.edu.au.
    All software and documents on the Pizza site are © Copyright 1996, 1997 by the respective authors (as attributed on each; terms for redistribution are available).